﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CGSR.Core;

namespace CGSR.Simulation
{
    class Program
    {
        
        static void Main(string[] args)
        {
        	string scenariosPath = @"..\..\Benchmarks\";
			string[] movePatterns = { "Nomadic", "RandomWalk" };

			var resultsdir = new DirectoryInfo(scenariosPath + @"\simulation results\");
			if (!resultsdir.Exists)
				resultsdir.Create();

			DirectoryInfo trafficDir=new DirectoryInfo(scenariosPath+@"\Traffic");

			var statisticsFile = new StreamWriter(resultsdir.FullName+"total statistics.csv", false);
        	statisticsFile.AutoFlush = true;

			statisticsFile.WriteLine("Scenario number,Movement pattern,Users,Ticks,maxX,maxY,MinPktSize,MaxPktSize,ReceptionRadius,Power,QueueSize,Packet loss percentage, Average latency");

        	foreach (var trafficFile in trafficDir.GetFiles())
        	{
				int scenarioNum = int.Parse(trafficFile.Name.Split('_')[1]);

				Console.WriteLine("Starting scenario number " + scenarioNum);

        	    FileInfo file = trafficFile;
//        	    Parallel.ForEach(movePatterns,
//        	                     movePattern =>
//        	                     runScenario(scenarioNum, statisticsFile, file, movePattern, scenariosPath));
        		foreach (string movePattern in movePatterns)
        		{
					runScenario(scenarioNum, statisticsFile, trafficFile, movePattern, scenariosPath);
        		}
				Console.WriteLine("Finished scenario number "+scenarioNum);

        	}

			statisticsFile.Close();
        	Console.WriteLine("All done! Press any key to cuntinue.");
			Console.ReadKey(true);
        }

        private static void runScenario(int scenarioNum, StreamWriter statisticsFile, FileInfo trafficFile, string movePattern,
                                        string scenariosPath)
        {
            Console.WriteLine("	Starting move pattern: " + movePattern);
            var locationsFile = Directory.GetFiles(scenariosPath + movePattern, movePattern + "_" + scenarioNum + "_*.*")[0];

            BenchmarkParser bp = new BenchmarkParser(locationsFile, trafficFile.FullName);
//        			Console.WriteLine("     Finished parsing for "+ movePattern);

            SimulationManager sm = new SimulationManager();
            var results = sm.RunSimulation(bp.LocationsManager, bp.PacketsManager,
                                           scenariosPath + @"\simulation results\" + movePattern + scenarioNum + ".csv");
            statisticsFile.WriteLine(scenarioNum + "," + movePattern + "," + bp.LocationsManager.NumOfUsers
                                     + "," + sm.NumOfTicks + "," + bp.LocationsManager.MaxX + "," + bp.LocationsManager.MaxY +
                                     "," +
                                     bp.PacketsManager.MinPacketSize + "," + bp.PacketsManager.MaxPacketSize + "," +
                                     sm.receptionRadius + "," +
                                     bp.PacketsManager.Power + "," + bp.PacketsManager.QueueSize + "," + results.Item1 + "," +
                                     results.Item2);
            Console.WriteLine("	Finished move pattern: " + movePattern);
        }
    }
}
